﻿namespace NJQuery.Configuration {
	using System.Configuration;
	using System.Diagnostics.CodeAnalysis;

	/// <summary>
	/// Represents a collection of <see cref="Theme"/> objects.
	/// This class cannot be inherited.
	/// </summary>
	[SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface", Justification = "If the framework didn't implement the generic interface, I'm not going to.")]
	public class ThemeCollection : ConfigurationElementCollection {


		#region Constructors

		/// <summary>
		/// This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
		/// Initializes a new instance of the <see cref="ThemeCollection"/> class.
		/// </summary>
		public ThemeCollection() { }

		#endregion


		#region Indexers


		/// <summary>
		/// Gets or sets the <see cref="Theme"/> at a particular position.
		/// </summary>
		/// <param name="index">The zero-based index to get or set.</param>
		/// <returns>The <see cref="Theme"/> at the index specified by <paramref name="index"/>.</returns>
		public Theme this[int index] {
			get { return BaseGet(index) as Theme; }
			set {
				if(base.BaseGet(index) != null) BaseRemoveAt(index);
				BaseAdd(index, value);
			}
		}

		/// <summary>
		/// Gets a <see cref="Theme"/> in the collection with a matching <see cref="Theme.Name"/> property value.
		/// </summary>
		/// <param name="name">The invariant theme name to look up.</param>
		/// <returns>The first <see cref="Theme"/> in the collection matching the provided <paramref name="name"/>.  A null value is returned if no matches are found.</returns>
		public new Theme this[string name] {
			get {
				foreach(Theme theme in this)
					if(theme.Name == name) return theme;
				return null;
			}
		}

		#endregion


		#region Methods: Public

		/// <summary>
		/// Returns whether or not the collection contains a theme with a matching <see cref="Theme.Name"/> property value.
		/// </summary>
		/// <param name="name">The invariant theme name to look up.</param>
		/// <returns>Whether or not a matching <paramref name="name"/> was found.</returns>
		public bool Contains(string name) {
			return this[name] != null;
		}

		/// <summary>
		/// Adds a <see cref="Theme"/> object to the <see cref="ThemeCollection"/> instance.
		/// </summary>
		/// <param name="theme">The <see cref="Theme"/> object to add.</param>
		public void Add(Theme theme) {
			BaseAdd(theme);
		}

		/// <summary>
		/// Removes all <see cref="Theme"/> objects from a <see cref="ThemeCollection"/> object.
		/// </summary>
		public void Clear() {
			BaseClear();
		}

		/// <summary>
		/// Removes the specified <see cref="Theme"/> from the <see cref="ThemeCollection"/> object.
		/// </summary>
		/// <param name="theme">The <see cref="Theme"/> object to remove.</param>
		public void Remove(Theme theme) {
			if(BaseIndexOf(theme) >= 0)
				BaseRemove(theme.Name);
		}

		#endregion


		#region Methods: Protected Overrrides

		/// <summary>
		/// Creates a new <see cref="ConfigurationElement"/> object.
		/// </summary>
		/// <returns>A new <see cref="ConfigurationElement"/> object.</returns>
		protected override ConfigurationElement CreateNewElement() {
			return new Theme();
		}

		/// <summary>
		/// Gets the element key for the specified <see cref="ConfigurationElement"/> object.
		/// </summary>
		/// <param name="element">The <see cref="ConfigurationElement"/> object whose key you want to return.</param>
		/// <returns>The object acts as the key for the specified <see cref="ConfigurationElement"/> instance.</returns>
		protected override object GetElementKey(ConfigurationElement element) {
			return ((Theme)element).Name;
		}

		#endregion


	}

}